extends Node2D

@onready var player: Ark_Player = $Player

const AUTO_MOVE_NONE = 0
const AUTO_MOVE = 1
const AUTO_MOVE_FIN = 2
const AUTO_MOVE_SPEED: float = 50
var auto_move_state = AUTO_MOVE_NONE

const SPAWN_POINT: Vector2 = Vector2(0, 96)

@onready var npc: Npc = $Npc
@onready var pointer_mark: PointerMark = $PointerMark
@onready var option: Label = $Option
@onready var bubble_left: TileMapLayer = $BubbleLeft
@onready var bubble_right: TileMapLayer = $BubbleRight
@onready var bubble_text_left: Label = $BubbleLeft/Label
@onready var bubble_text_right: Label = $BubbleRight/Label
@onready var mark: InvisibleMark = $InvisibleMark

@onready var fade_text: Label = $FadeText
const FADE_NONE : int = 0
const FADE_IN : int = 1
const FADE_IN_FIN : int = 2
const FADE_OUT : int = -1
const FADE_OUT_FIN : int = -2
const FADE_IN_TIME : float = 1.0
const FADE_OUT_TIME : float = 1.0
var fade_state : int = FADE_NONE
var elapsed : float = 0
var space_pressed : bool = false 

const BUBBLE_FADE_IN_TIME: float = 2.0

func start_fade_in():
	fade_state = FADE_IN

func start_fade_out():
	fade_state = FADE_OUT
	fade_text.visible = false
	
func show_options():
	option.visible = true

func hide_options():
	option.visible = false

func lets_smile():
	player.animation.play("smile")
	npc.animation.play("smile")
	get_tree().create_timer(1.5).timeout.connect(lets_go)

func lets_go():
	print("lets go")
	player.controllable = true
	npc.flip_h = not npc.flip_h
	npc.rel_dist = npc.position.x - player.position.x
	player.companion.append(npc)

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	BgmMgr.audioPlayer.stream = BgmMgr.bgm4
	BgmMgr.audioPlayer.play()
	player.controllable = false
	auto_move_state = AUTO_MOVE
	player.velocity.x = AUTO_MOVE_SPEED
	player.position = SPAWN_POINT
	
	pointer_mark.player_stopped.connect(start_fade_in)
	fade_text.modulate.a = 0
	
	option.visible = false
	
	bubble_left.modulate.a = 0
	bubble_right.modulate.a = 0
	bubble_list.append(bubble_right)
	bubble_list.append(bubble_left)
	bubble_text_list.append(bubble_text_right)
	bubble_text_list.append(bubble_text_left)
	
	mark.player_stopped.connect(lets_smile)
	
	for bubble in bubble_list:
		bubble.modulate.a = 0
	for text in bubble_text_list:
		text.modulate.a = 0

const BUBBLE_FADE_NONE = 0
const BUBBLE_FADE_IN = 1
const BUBBLE_FADE_IN_FIN = 2
var bubble_fade_state = BUBBLE_FADE_NONE
var current_bubble_index = 0
var bubble_list: Array[TileMapLayer] = []
var bubble_text_list: Array[Label] = []
var alpha: float = 0.0

var right_visible: bool = false

func bubble_fade_in(delta: float) -> void:
	if bubble_fade_state == BUBBLE_FADE_IN:
		bubble_list[current_bubble_index].enabled = true
		elapsed += delta
		alpha = min(elapsed, BUBBLE_FADE_IN_TIME) / BUBBLE_FADE_IN_TIME
		bubble_list[current_bubble_index].modulate.a = alpha
		bubble_text_list[current_bubble_index].modulate.a = alpha
		if bubble_list[current_bubble_index].modulate.a == 1:
			if current_bubble_index == 0:
				right_visible = true
				show_options()
			current_bubble_index += 1
			elapsed = 0
			bubble_fade_state = BUBBLE_FADE_IN_FIN	
			if current_bubble_index == 2:
				player.controllable = true

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if auto_move_state == AUTO_MOVE and player.position.x > 35:
		player.velocity.x = 0
		player.controllable = true
		auto_move_state = AUTO_MOVE_FIN

	space_pressed = Input.is_action_just_pressed("space")
	
	if fade_state == FADE_IN:
		if space_pressed:
			fade_text.modulate.a = 1
			fade_state = FADE_IN_FIN
			start_fade_in()
			bubble_fade_state = BUBBLE_FADE_IN
			elapsed = 0
		
			return
		else:
			elapsed += delta
			fade_text.modulate.a = min(1, elapsed) / FADE_IN_TIME
			
			if fade_text.modulate.a >= 1:
				fade_state = FADE_IN_FIN
				bubble_fade_state = BUBBLE_FADE_IN
				elapsed = 0
				
			return
	
	if space_pressed and right_visible:
		bubble_fade_state = BUBBLE_FADE_IN
		start_fade_out()
		hide_options()
	
	if bubble_fade_state == BUBBLE_FADE_IN:
		bubble_fade_in(delta)
	
	if player.position.x >= 416:
		get_tree().change_scene_to_file("res://karte/karte_fin.tscn")
	
	if player.position.y >= 257:
		player.position = SPAWN_POINT
